﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.Bussiness;
using FB.DataAccess;
using FB.Utilities;

namespace FB.admin
{
    public partial class VideoCategoryModify : System.Web.UI.Page
    {
        /// <summary>
        ///  View State EditItemID
        /// </summary>
        private Guid? EditItemID
        {
            get
            {
                object obj = this.ViewState["_EditItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_EditItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadForm();
            }
        }

        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR)
                || Page.User.IsInRole(Constants.ROLE_AUTHOR)
                || Page.User.IsInRole(Constants.ROLE_CONTRIBUTER)
                || Page.User.IsInRole(Constants.ROLE_EDITOR))
            {
                //
            }
            else
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadForm()
        {
            Guid TempID;
            if (Guid.TryParse(Request.QueryString["ID"], out TempID))
            {
                EditItemID = TempID;
            }
            if (EditItemID != null)
            {
                LoadFormEdit();
            }
            else
            {
                LoadFormAdd();
            }
        }


        private void LoadFormAdd()
        {
            lblTitle.Text = "Video: Add New";
        }

        private void LoadFormEdit()
        {
            lblTitle.Text = "Video: Edit";
            VideoCategoryBO BO = new VideoCategoryBO();
            VideoCategory CurrentVideoCat = BO.GetVideoCategoryById((Guid)EditItemID);
            if (CurrentVideoCat != null)
            {
                txtName.Text = CurrentVideoCat.Name;
                txtDescription.Text = CurrentVideoCat.Description;
                chkEnable.Checked = (bool)CurrentVideoCat.Enable;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveForm();
            //TODO: Show msg
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveForm();
            Response.Redirect("VideoCategoryList.aspx");
        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            SaveForm();
            EditItemID = null;
            Response.Redirect("VideoCategoryModify.aspx?ID=0");
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("VideoCategoryList.aspx");
        }
        private void SaveForm()
        {
            VideoCategoryBO BO = new VideoCategoryBO();
            //Edit

            if (EditItemID != null)
            {
                VideoCategory EditVideoCat = BO.GetVideoCategoryById((Guid)EditItemID);
                EditVideoCat.Name = txtName.Text;
                EditVideoCat.Description = txtDescription.Text;
                EditVideoCat.Enable = chkEnable.Checked;
                BO.UpdateVideoCategory(EditVideoCat);
            }
            else
            {
                VideoCategory NewVideoCate = new VideoCategory();
                NewVideoCate.ID = Guid.NewGuid();
                NewVideoCate.Name = txtName.Text;
                NewVideoCate.Description = txtDescription.Text;
                NewVideoCate.Enable = chkEnable.Checked;
                BO.InsertVideoCategory(NewVideoCate);
            }
        }
    }
}